#import "AutohideDock.h"
#import <Carbon/Carbon.h>

@implementation AutohideDock
{
    NSStatusItem* mStatusItem;
    id mEventMonitor;
}

//init
- (instancetype)init
{
    self = [super init];
    if(self)
    {
        mStatusItem = nil;
        mEventMonitor = nil;
    }
    
    return self;
}

//程序菜单加入到Mac状态栏
- (void)addStatusBarMenu
{
    NSMenuItem* menuItem = [[NSMenuItem alloc] init];
    menuItem.title = @"退出";
    menuItem.target = self;
    menuItem.action = @selector(onExitHandler);
    
    NSMenu* menu = [[NSMenu alloc] init];
    [menu addItem:menuItem];
    
    NSImage* image = [NSImage imageNamed:@"MenuIcon"];
    image.size = NSMakeSize(15, 10);
    
    mStatusItem = [NSStatusBar.systemStatusBar statusItemWithLength:NSSquareStatusItemLength];
    mStatusItem.menu = menu;
    mStatusItem.button.image = image;
}

//监听全局事件
- (void)addMonitor
{
    mEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskFlagsChanged handler:^(NSEvent* event)
    {
        [self onEventMonitorHandler:event];
    }];
}

//移除监听全局事件
- (void)removeMonitor
{
    [NSEvent removeMonitor:mEventMonitor];
}

//得到自动隐藏Dock的bool值
- (BOOL)getAutohide
{
    NSString* script = @"tell application \"System Events\" to get the autohide of the dock preferences";
    return [self runAppleScript:script];
}

//设置自动隐藏Dock
- (void)setAutohide:(BOOL)value
{
    NSString* script = nil;
    if(value)
        script = @"tell application \"System Events\" to set the autohide of the dock preferences to true";
    else
        script = @"tell application \"System Events\" to set the autohide of the dock preferences to false";
    
    [self runAppleScript:script];
}

//设置自动隐藏Dock延时的值为3600000(1000小时)
- (void)setAutohideDelay
{
    NSString* script = @"do shell script \"defaults write com.apple.dock autohide-delay -int 3600000\"";
    [self runAppleScript:script];
}

//设置自动隐藏Dock动画的值为0
- (void)setAutohideTimeModifier
{
    NSString* script = @"do shell script \"defaults write com.apple.dock autohide-time-modifier -int 0\"";
    [self runAppleScript:script];
}

//恢复自动隐藏Dock延时和动画，同时取消自动隐藏Dock
- (void)resetAutohideDelayAndTimeModifier
{
    NSString* delayScript = @"do shell script \"defaults delete com.apple.dock autohide-delay\"";
    NSString* timeModifierScript = @"do shell script \"defaults delete com.apple.dock autohide-time-modifier\"";
    [self runAppleScript:delayScript];
    [self runAppleScript:timeModifierScript];
    [self setAutohide:NO];
}

//重启Dock
- (void)resetDock
{
    NSString* script = @"do shell script \"killall Dock\"";
    [self runAppleScript:script];
}

//运行Apple脚本
- (BOOL)runAppleScript:(NSString* )script
{
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithSource:script];
    NSAppleEventDescriptor* descriptor = [appleScript executeAndReturnError:nil];
    
    return descriptor.booleanValue;
}

//监听全局事件
- (void)onEventMonitorHandler:(NSEvent* )event
{
    //监听Function键
    if(event.keyCode == kVK_Function)
    {
        //判断是否按下Function键
        if((event.modifierFlags & NSEventModifierFlagFunction) != 0)
        {
            //当Function键按下时，如果Dock隐藏了，则显示出来
            if([self getAutohide])
                [self setAutohide:NO];
        }
        else
        {
            //当Function键松开时，如果Dock显示了，则隐藏出来
            if(![self getAutohide])
                [self setAutohide:YES];
        }
    }
}

//退出程序
- (void)onExitHandler
{
    [NSApplication.sharedApplication terminate:nil];
}

@end
